<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../base.jsp" %>
<!-- Select2 -->
<link rel="stylesheet" href="${url }plugins/select2/select2.min.css">
<script src="${url }plugins/select2/select2.full.min.js"></script>
<style>
	.content-wrapper{margin-left: 0px}
</style>
<script type="text/javascript">
$(function() {
	init();
	init2();
});
function init(){
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'manager/getUserList',
		data:$('#userForm').serialize(),
		success: function(result){
			console.dir(result);
			$("#userTbody").html("");
			if(result){
				for(var i=0;i<result.result.content.length;i++){
					var appentStr="<tr><td>"+result.result.content[i].id+"</td>";
					appentStr+="<td>"+result.result.content[i].account+"</td>";
					appentStr+="<td>"+result.result.content[i].name+"</td>";
					appentStr+="<td>"+result.result.content[i].addtime+"</td>";
					appentStr+="<td><a href='#' class='fa fa-edit' onclick='editUser("+result.result.content[i].id+")'>编辑</a><a href='' class='fa fa-remove' onclick='delUser("+result.result.content[i].id+","+result.result.content[i].type+",\""+result.result.content[i].account+"\")'>删除</a></td></tr>";
					$("#userTbody").append(appentStr);
				}
			}
		}
	}); 
}

function init2(){
	var count=$("#selectPageCount").val();
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'manager/getUserListCount',
		data:$('#userForm').serialize(),
		success: function(result){
			if(result){
				var dataNum = result.result.content[0].count;
				var countForPage=0;
				if(dataNum!=0){
					if(parseInt(dataNum)/parseInt(count)>0){
						if(parseInt(dataNum)%parseInt(count)==0){
							countForPage = parseInt(parseInt(dataNum)/parseInt(count));
						}else{
							countForPage = parseInt(parseInt(dataNum)/parseInt(count))+parseInt(1);
						}
					}else{
						countForPage=1;
					}	
				}
				$("#pages").val(countForPage);
				$("#pagination").html("");
				var appendStr="<li><a href='#' onclick='goPage(-1)'>&laquo;</a></li>";
				for(var i=1;i<=countForPage;i++){
					appendStr+="<li><a href='#' onclick='goPage("+i+")'>"+i+"</a></li>";
				}
				appendStr+="<li><a href='#' onclick='goPage(0)'>&raquo;</a></li>";
				$("#pagination").append(appendStr);
			}
		}
	}); 
}

function goPage(index){
	if(index==-1){
		if($("#currentPage").val()!=1){
			$("#currentPage").val(parseInt($("#currentPage").val())-1);
		}else{
			alert("已经到第一页了");
			return;
		}
	}else if(index==0){
		if($("#currentPage").val()!=$("#pages").val()){
			$("#currentPage").val(parseInt($("#currentPage").val())+1);
		}else{
			alert("已经到最后一页了");
			return;
		}
	}else if(index==-2){
			$("#pageCount").val($("#selectPageCount").val());
			$("#currentPage").val(1);
	}else{
		$("#currentPage").val(index);
	}
	
	$.ajax({
		type:"POST",
		url: path+'manager/getUserList',
		data:$('#userForm').serialize(),
		success: function(result){
			$("#userTbody").html("");
			if(result){
				for(var i=0;i<result.result.content.length;i++){
					var appentStr="<tr><td>"+result.result.content[i].id+"</td>";
					appentStr+="<td>"+result.result.content[i].account+"</td>";
					appentStr+="<td>"+result.result.content[i].name+"</td>";
					appentStr+="<td>"+result.result.content[i].addtime+"</td>";
					appentStr+="<td><a href='#' class='fa fa-edit' onclick='editUser("+result.result.content[i].id+")'>编辑</a><a href='' class='fa fa-remove' onclick='delUser("+result.result.content[i].id+","+result.result.content[i].type+",\""+result.result.content[i].account+"\")'>删除</a></td></tr>";
					//appentStr+="<td><a href='' class='fa fa-edit'>编辑</a><a href='' class='fa fa-remove'>删除</a></td></tr>";
					$("#userTbody").append(appentStr);
				}
			}
		}
	}); 
	init2();
}

function delUser(id,type,account){
	if(confirm('确实要删除吗?')){
		$.ajax({
			cache:false,
			async:false,
			type:"POST",
			url: path+'manager/delUser',
			data:{id:id,type:type,account:account},
			success: function(result){
				if(result){
					alert("删除成功");
				}
			}
		});
	}
}

function editUser(id){
	location.href=path+"manager/editUser?id="+id;
}
</script>
<body class="hold-transition skin-blue sidebar-mini">

	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
			<h1>用户管理<small>用户列表</small></h1>
				<ol class="breadcrumb">
					<li><a href="javascript:;"><i class="fa fa-dashboard"></i> 首页</a></li>
					<li class="active">用户管理</li>
				</ol>
			</section>
			<!-- Main content -->
			<section class="content">
				<div class="row">
				<div class="col-xs-12">
					<div class="box">
						<!-- /.box-header -->
						<form id="userForm">
						<div class="box-body table-responsive no-padding">
						<table class="table table-hover">
							<tr>
								<td width="10%"><a href="${url }manager/toEdit" class="btn btn-primary">新增</a></td>
								<td width="20%">
									角色：<select name="sysRole" >
										<option value="">==全部==</option>
										<option value="0">普通用户</option>
										<option value="1">配置管理员</option>
										<option value="2">审计管理员</option>
										<option value="3">系统管理员</option>
									</select>
								</td>
								<td width="30%">账号：
									<input type="text" name="account">
									<input  type="hidden" name="currentPage"  value="1"  id="currentPage" />
									<input  type="hidden" name="pageCount"  value="10"  id="pageCount" />
									<input  type="hidden" name="pages"  id="pages" />
								</td>
								<td width="20%">
								用户类型：<select id="userType" name="userType">
										              	<option value="1" selected="selected">管理员</option>
										              	<option value="0">普通用户</option>
										           </select>
								</td>
								<td width="5%"><a type="button" class="btn btn-success" onclick="goPage(-2)">查询</a></td>
							</tr>
						</table>
						</div>
						</form>
					</div>
				</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
					<select id="selectPageCount" class="selectpicker" data-style="btn-primary" onchange="goPage(-2)">
					<option value="10" selected="selected">10</option>
					<option value="25">25</option>
					<option value="50">50</option>
					</select>记录/页
						<div class="box">
							<!-- /.box-header -->
							<div class="box-body table-responsive no-padding">
								<table class="table table-bordered">
									<tr>
										<th>ID</th>
										<th>账号</th>
										<th>名称</th>
										<th>创建时间</th>
										<th>操作</th>
									</tr>
									<tbody id="userTbody">
									
									</tbody>
								</table>
							</div>
							<ul class="pagination" id="pagination">

				</ul>
							<!-- /.box-body -->
						</div>
						<!-- /.box -->
					</div>
					
				</div>
			</section>
		</div>
	</div>
	<script type="text/javascript">
	$(function () {
		$(".select2").select2();
		//iCheck for checkbox and radio inputs
	    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
	      checkboxClass: 'icheckbox_minimal-blue',
	      radioClass: 'iradio_minimal-blue'
	    });
	});
	</script>
</body>